package com.hulk.dryad.manage.config;

import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;


import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.hulk.dryad.common.jackson.module.HulkBigNumberModule;
import com.hulk.dryad.common.jackson.module.HulkJavaTimeModule;
import com.hulk.dryad.manage.framework.xss.core.JacksonXssClean;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Locale;
import java.util.TimeZone;

/**
 * JacksonConfig 配置时间转换规则
 * {@link HulkJavaTimeModule}、默认时区等
 *
 * @author hulk
 * @date 2020-06-15
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(ObjectMapper.class)
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class JacksonConfig {

	private static final String ASIA_SHANGHAI = "Asia/Shanghai";

	@Bean
	@ConditionalOnMissingBean
	public Jackson2ObjectMapperBuilderCustomizer customizer() {
		return builder -> {
			// xss 防注入过滤
			//builder.deserializerByType(String.class, new JacksonXssClean());

			builder.locale(Locale.CHINA);
			builder.timeZone(TimeZone.getTimeZone(ASIA_SHANGHAI));
			builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
			//mybatis-plus 序列化枚举值为数据库存储值
			builder.featuresToEnable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
			builder.modules(
					new HulkJavaTimeModule(),
					new HulkBigNumberModule()
			);
		};
	}

}
